<template>
  <div>
    <div class="heard-right">
      <stageList
        width="250px"
        class="inline mr10"
        @stageCallback = 'stageCallback'></stageList>
    </div>
    <div class="second-right">
      <span style="line-height: 30px; margin-right: 10px">数据来源：{{dataName}}</span>
      <button class="button-block" v-show="boardSetupMaintain && dataType === 1" @click="exportData">导入检查记录</button>
      <button class="button-block" v-show="boardSetupMaintain" @click="changeType">改变为{{exportaName}}</button>
    </div>
    <div class="p20">
      <div class="tableClass-layered mt15">
        <table style="width: 100%;text-align: center">
          <tr>
            <th rowspan="2">统计日期</th>
            <th class="border-left" colspan="2">质量问题</th>
            <th class="border-left" colspan="2">安全问题</th>
            <th rowspan="2">更新时间</th>
            <th v-if="boardSetupMaintain" rowspan="2">操作</th>
          </tr>
          <tr>
            <th>新增问题</th>
            <th>待整改</th>
            <th>新增问题</th>
            <th>待整改</th>
          </tr>
          <tr v-if="dataList.length === 0">
            <td colspan="7">
              <zz-none style="width: 100%" content="暂无数据"></zz-none>
            </td>
          </tr>
          <tr v-else v-for="(item, index) in dataList" :id="item.qualityAndSafetyId">
            <td>{{item.statDate}}</td>
            <td>{{item.qualityProblemNew}}</td>
            <td>{{item.qualityProblemEdit}}</td>
            <td>{{item.safeProblemNew}}</td>
            <td>{{item.safeProblemEdit}}</td>
            <td>{{item.gmtCreate}}</td>
            <td v-if="boardSetupMaintain"><span class="handle-td" @click="reduceData(index)">删除</span></td>
          </tr>
        </table>
        <zz-paginate v-if="dataList.length !== 0" @choose="findQualitySafetyList()"></zz-paginate>
      </div>
    </div>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  export default {
    name: '',
    data () {
      return {
        tableTilte: [
          {name: '分类', width: '33%', align: 'left'},
          {name: '新增', width: '33%', align: 'left'},
          {name: '待整改', width: '33%', align: 'left'}
        ],
        dataList: [],
        lastName: '',
        dataName: '',
        exportaName: '',
        dataType: 1, // 1:手工导入, 2:同步云管控系统
        stageId: '',
        boardSetupMaintain: false
      }
    },
    computed: {
      ...mapGetters({
        currentUser: 'currentUser',
        stageList: 'getstageList',
        stageListDefault: 'getstageListDefault'
      })
    },
    watch: {
      stageId (val) {
        this.setDefault()
        this.findQualitySafetyList(val)
      },
    },
    mounted () {
      this.getAuth()
      this.stageId = this.stageListDefault.stageId
    },
    methods: {
      getAuth () {
        let data = {
          'query.project_id': this.currentUser.pjId,
          'query.fun_code': 'p_BoardSetup_data_maintain'
        }
        this.$axios({
          method: 'post',
          url: `${this.$site.companyServer}/front/hasFunction.htm`,
          data: this.$qs.stringify(data)
        }).then(res => {
          this.boardSetupMaintain = res.data.hasFunction
        })
      },
      findQualitySafetyList (val) {
        let stageId
        if (val) {
          stageId = val
        } else {
          stageId = this.stageId
        }
        let data = {
          'stageId': stageId
        }
        this.$axios({
          method: 'post',
          url: `${this.$site.videoServer}/api/qualitySafety/findQualitySafetyList`,
          data: this.$qs.stringify(data, {indices: false}),
          paginate: true
        }).then(res => {
          this.dataList = res.data.data.dataList
        })
      },
      reduceData (index) {
        this.$axios({
          method: 'delete',
          url: `${this.$site.videoServer}/api/qualitySafety/deleteQualitySafety?qualitySafetyId=${this.dataList[index].qualityAndSafetyId}`
        }).then(res => {
          if (res.data.errorMsg) {
            this.$message.error(res.data.errorMsg)
          } else {
            this.$message.success(res.data.data)
            this.findQualitySafetyList(this.stageId)
          }
        })
      },
      setDefault () {
        let data = {
          'stageId': this.stageId
        }
        this.$axios({
          method: 'post',
          url: `${this.$site.videoServer}/api/qualitySafety/selectQualityAndSafetyType`,
          data: this.$qs.stringify(data, {indices: false})
        }).then(res => {
          if (res.data.errorMsg) {
            this.$message.error(res.data.errorMsg)
          } else {
            this.dataType = res.data.data
            this.setData(this.dataType)
          }
        })
      },
      setData (dataType) {
        switch (dataType) {
          case 1:
            this.lastName = '同步云管控系统'
            this.dataName = '手工导入'
            this.exportaName = '数据同步'
            break;
          case 2:
            this.lastName = '手工导入'
            this.dataName = '同步云管控系统'
            this.exportaName = '手工导入'
            break;
        }
      },
      changeType () {
        this.$MessageBox({
          showClose: true,
          message: `是否将质量、安全管理的数据来源改为【${this.lastName}】?`,
          type: 'info',
          confirmHanler: () => {
            switch (this.dataType) {
              case 1:
                this.dataType = 2
                this.setData(2)
                this.changeSourceType(2)
                break;
              case 2:
                this.dataType = 1
                this.setData(1)
                this.changeSourceType(1)
                break;
            }
          }
        })
      },
      changeSourceType (dataType) {
        let data = {
          'sourceType': dataType,
          'stageId': this.stageId
        }
        this.$axios({
          method: 'post',
          url: `${this.$site.videoServer}/api/qualitySafety/settingExportType`,
          data: this.$qs.stringify(data, {indices: false})
        }).then(res => {
          if (res.data.errorMsg) {
            this.$message.error(res.data.errorMsg)
          } else {
            this.$message.success('修改成功！')
            this.findQualitySafetyList(this.stageId)
          }
        })
      },
      exportData () {
        this.$import.open({
          ftlType: 'qualitySafe',
          stageId: this.stageId,
          templateUrl: `${this.$site.videoServer}/api/qualitySafety/downloadQualitySafety`,
          actionUrl: `${this.$site.videoServer}/api/qualitySafety/exportQualitySafety`
        }).then(res => {
          this.findQualitySafetyList()
        })
      },
      stageCallback (val) {
        this.stageId = val.key
      }
    }
  }
</script>
<style lang="scss" scoped>
  .heard-right{
    position: absolute;
    top: 65px;
    right: 20px;
  }
  .second-right{
    position: absolute;
    top: 118px;
    right: 20px;
  }
  .handle-td{
    color: $color;
    cursor: pointer;
    &:hover{
      color: $color;
    }
  }
</style>
